<!DOCTYPE html>
<html lang="en">
<head>
    <title>自定义 - 创建实体</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="xms">
    <meta name="author" content="xms">
    <link rel="icon" href="/content/favicon.ico">

    <!-- Bootstrap core CSS -->
    <link href="/content/css/bootstrap3.3.5/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="/content/css/dashboard.css" rel="stylesheet">
    <link href="/content/css/font-awesome.min.css" rel="stylesheet">
    <link href="/content/customize/css/common.css" rel="stylesheet">
    <link href="/content/js/jquery-toast/jquery.toast.min.css" rel="stylesheet">
    <link id="themeLink" href="/content/css/theme/default.css" rel="stylesheet"/>
    <!-- Bootstrap core JavaScript ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="/content/js/jquery.min.js"></script>
    <script src="/content/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="/content/js/ie10-viewport-bug-workaround.js"></script>
    <script src="/content/js/jquery.bootstrap.min.js"></script>
    <script src="/content/js/json2.js"></script>
    <script src="/content/js/xms.utility.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.core.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.widget.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.mouse.js"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.draggable.js"></script>
    <script src="/content/js/jquery-toast/jquery.toast.min.js"></script>
    <script src="/content/js/xms.jquery.js"></script>
    <script src="/content/js/xms.web.js"></script>
    <script src="/content/js/xms.metadata.js"></script>
    <script src="/content/js/md5.js" type="text/javascript"></script>
    <script src="/content/js/commonlabels.js"></script>
    <script src="/content/js/common/pagecontext.js"></script>
    <script src="/api/user/usercontext"></script>
    <script>
        jQuery(function () {
            Xms.Web.Loading();

            function aLinkClick(e) {

                if (currentKeycode != 17 && window.parent && window.parent.addToTab) {
                    e.preventDefault();
                    var tabNav = $('#customTabNav', parent.document).children('.iframe-link-linkitem.active');
                    var textEl = tabNav.clone();
                    $(".glyphicon-remove", textEl).remove();
                    var label = textEl.text();
                    textEl = $(this).clone();
                    $("span", textEl).remove();
                    label = label + '-' + textEl.text();
                    var link = $(this).attr("href");
                    var id = $.md5(link);
                    Xms.Web.callParentMethod('addToTab', {id: id, label: label, link: link});
                    return false;
                } else {
                    //  currentKeycode = -99999;
                    //  var link = $(this).attr("href");
                    //  Xms.Web.OpenWindow(link);
                }

            }

            var currentKeycode = -99999;
            $("body").on("click.customizeLinkclick", 'a[href]a:not([href^="javascript:"])a:not([href^="#"])', function (e) {
                aLinkClick.call(this, e);
            });
        });
    </script>
    <style>
        body {
            padding: 0;
            margin: 0;
        }

        .table-responsive {
            max-height: 400px;
            overflow-y: auto;
        }

        .panel-default .panel-heading {
            background-color: white;
        }
    </style>
</head>
<body id="body">
<div class="clearfix">
    <div class="col-md-12" id="main">
        <div class="" style="padding:5px;" id="renderBody">
            <!--main-->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <a data-toggle="collapse"
                           href="#collapseTwo">
                            <strong>创建实体</strong>
                        </a>
                    </h3>
                </div>
                <div id="collapseTwo" class="panel-collapse collapse in">
                    <div class="panel-body">
                        <form action="/xms/api/schema/entity/create" data-jsonajax="true" data-formdata="false" method="post" id="editform" class="form-horizontal"
                              role="form" data-autoreset="true">
                            <ul id="myTab" class="nav nav-tabs">
                                <li class="active">
                                    <a href="#common" data-toggle="tab">
                                        实体信息
                                    </a>
                                </li>
                                <li>
                                    <a href="#defaults" data-toggle="tab">
                                        默认创建
                                    </a>
                                </li>
                            </ul>
                            <div id="myTabContent" class="tab-content">
                                <div id="common" class="tab-pane fade in active" style="padding:5px;">
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="name">名称</label>
                                        <div class="col-sm-10">
                                            <input autofocus="true" class="form-control required"
                                                   data-custom="entitieName" data-custommsg="首字符为字母，只能输入数字，字母或者下划线"
                                                   data-customreg="^[a-zA-Z]+[\w_]*$" data-val="true"
                                                   data-val-required="必填项" entitiename="true" id="name" name="name"
                                                   type="text" value="" aria-required="true">
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="localizedname">显示名称</label>
                                        <div class="col-sm-10">
                                            <input class="form-control required" data-val="true" data-val-required="必填项"
                                                   id="localizedname" name="localizedname" type="text"
                                                   aria-required="true">
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="ParentEntityId">上级实体</label>
                                        <div class="col-sm-10">
                                            <input class="form-control" id="ParentEntityId" name="parententityid"
                                                   type="text">
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="EntityMask1">授权级别</label>
                                        <div class="col-sm-10">
                                            <label class="checkbox-inline">
                                                <input checked="checked" class="required" data-val="true"
                                                       id="EntityMask1" name="entitymask" type="radio" value="1"
                                                       aria-required="true"> 按用户
                                            </label>
                                            <label class="checkbox-inline">
                                                <input class="required" id="EntityMask2" name="entitymask" type="radio"
                                                       value="4" aria-required="true"> 按组织
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="AuthorizationEnabled1">权限控制</label>
                                        <div class="col-sm-10">
                                            <label class="checkbox-inline">
                                                <input class="required" data-val="true"
                                                       id="AuthorizationEnabled1" name="authorizationenabled"
                                                       type="radio" value="True" aria-required="true"> 启用
                                            </label>
                                            <label class="checkbox-inline">
                                                <input checked="checked" class="required" id="AuthorizationEnabled2"
                                                       name="authorizationenabled" type="radio" value="false"
                                                       aria-required="true"> 禁用
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="LogEnabled1">日志监控</label>
                                        <div class="col-sm-10">
                                            <label class="checkbox-inline">
                                                <input class="required" data-val="true"
                                                       id="LogEnabled1" name="logenabled" type="radio" value="true"
                                                       aria-required="true"> 启用
                                            </label>
                                            <label class="checkbox-inline">
                                                <input checked="checked" class="required" id="LogEnabled2"
                                                       name="logenabled" type="radio" value="false"
                                                       aria-required="true"> 禁用
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="DuplicateEnabled1">重复检测</label>
                                        <div class="col-sm-10">
                                            <label class="checkbox-inline">
                                                <input class="required" data-val="true"
                                                       id="DuplicateEnabled1" name="duplicateenabled" type="radio"
                                                       value="true" aria-required="true"> 启用
                                            </label>
                                            <label class="checkbox-inline">
                                                <input checked="checked" class="required" id="DuplicateEnabled2"
                                                       name="duplicateenabled" type="radio" value="false"
                                                       aria-required="true"> 禁用
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="WorkFlowEnabled1">启用审批流</label>
                                        <div class="col-sm-10">
                                            <label class="checkbox-inline">
                                                <input class="required" data-val="true"
                                                       id="WorkFlowEnabled1" name="workflowenabled" type="radio"
                                                       value="true" aria-required="true"> 启用
                                            </label>
                                            <label class="checkbox-inline">
                                                <input checked="checked" class="required" id="WorkFlowEnabled2"
                                                       name="workflowenabled" type="radio" value="false"
                                                       aria-required="true"> 禁用
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label">分组</label>
                                        <div class="col-sm-10" id="entitygroups">
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label" for="Description">描述</label>
                                        <div class="col-sm-10">
                                            <textarea class="form-control" id="Description" name="Description"
                                                      row="5"></textarea>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12 text-center" id="form-buttons">
                                        <button type="submit" class="btn btn-primary"><span
                                                class="glyphicon glyphicon-saved"></span> 保存
                                        </button>
                                        <button type="reset" class="btn btn-default"><span
                                                class="glyphicon glyphicon-refresh"></span> 重填
                                        </button>
                                    </div>
                                </div>
                                <div id="defaults" class="tab-pane fade" style="padding:5px;">
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label">默认表单</label>
                                        <div class="col-sm-10">
                                            <label><input type="hidden" name="createDefaultForm" value="true" />
                                                <input type="checkbox" onclick="$(this).prev().val($(this).prop('checked'))" checked /> 创建</label>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label">默认视图</label>
                                        <div class="col-sm-10">
                                            <label><input type="hidden" name="createDefaultView" value="true" />
                                                <input type="checkbox" onclick="$(this).prev().val($(this).prop('checked'))" checked /> 创建</label>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label">默认字段</label>
                                        <div class="col-sm-10" id="defaultAttributes">
                                            <div class="col-sm-12 btn-group btn-group-xs">
                                                <button type="button" class="btn btn-default">全选</button>
                                                <button type="button" class="btn btn-default">取消</button>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group col-sm-12">
                                        <label class="col-sm-2 control-label">默认按钮</label>
                                        <div class="col-sm-10" id="defaultButtons">
                                            <div class="col-sm-12 btn-group btn-group-xs">
                                                <button type="button" class="btn btn-default">全选</button>
                                                <button type="button" class="btn btn-default">取消</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

        </div>
        <nav class="navbar navbar-default navbar-fixed-bottom hide" role="navigation" id="body-footer">
            <div class="container">
                <div class="navbar-form navbar-right" id="body-footer-content">
                </div>
            </div>
        </nav>
    </div>
</div>
</div>
<script src="/content/js/jquery.form.js"></script>
<script src="/content/js/jquery-validate/jquery.validate.min.js"></script>
<script src="/content/js/jquery-validate/localization/messages_zh.min.js"></script>
<script>
    var model = $.urlParamObj(location.href);
    var entitygroupid = model.entitygroupid;
    $(function () {
        //表单验证
        Xms.Web.Form($("#editform"), function (response) {
            Xms.Web.Alert(response.IsSuccess, response.Content || response.ErrorMessage);
            if (response.IsSuccess) {
                $('#myTab').find('li:first').trigger('click');
                Xms.Web.Event.publish('refresh');
                Xms.Web.Event.publish('refreshtree');
                Xms.Web.Event.localStorageEvent.trigger('list_entity_rebind');

            }
        }, null, null, {onKeyup: true});
        //检查名称是否已存在
        $('#name').bind('change', function () {
            var $this = $(this);
            var name = $this.val();
            if (name != '') {
                Xms.Web.Get('/api/schema/entity/exists?&name=' + name, function (response) {
                    $this.next().remove();
                    $this.parents('.form-group').removeClass('has-error');
                    if (!response.IsSuccess) {
                        $this.after($('<div class="text-danger"><span class="glyphicon glyphicon-remove-sign"></span>' + (response.Content || '名称已存在') + '</div>'));
                        $this.parents('.form-group').addClass('has-error');
                    }
                });
            }
        });
        bindSystemAttributes();
        bindSystemButtons();
        loadEntities();
        bindEntityGroups();
    });

    function bindEntityGroups() {
        Xms.Ajax.GetJson('/api/data/retrieve/all/entitygroup/name/name:asc', null, function (response) {
            var data = Xms.Web.GetAjaxResult(response);
            var $container = $('#entitygroups');
            $(data.content).each(function (i, n) {
                $container.append('<label class="col-sm-3 col-xs-3"><input type="checkbox" name="entitygroupid" value="' + n.entitygroupid + '"'+(entitygroupid == n.entitygroupid ? ' checked' : '')+' />' + n.name + '</label>');
            });
        });
    }

    function bindSystemAttributes() {
        Xms.Ajax.GetJson('/api/schema/attribute/SystemAttributes/user', null, function (response) {
            console.log(response);
            if (response.IsSuccess) {
                var container = $('#defaultAttributes');
                var data = response.Content;
                $(data).each(function (i, n) {
                    if (n.attributetypename == 'primarykey') {
                        //n.localizedname = LOC_ATTRIBUTETYPE_PRIMARYKEY;
                        return true;
                    }
                    container.append($('<label class="col-sm-3"><input type="checkbox" name="defaultattributes" value="' + n.name + '" checked="checked" /> ' + n.localizedname + '</label>'));
                });
                container.find('button:eq(0)').click(function () {
                    container.find('input[type=checkbox]').prop('checked', true);
                });
                container.find('button:eq(1)').click(function () {
                    container.find('input[type=checkbox]').removeProp('checked');
                });
            } else {
                Xms.Web.Alert(response.Content);
            }
        })
    }

    function bindSystemButtons() {
        Xms.Ajax.GetJson('/api/ribbonbutton/SystemButtons/user', null, function (response) {
            console.log(response);
            if (response.IsSuccess) {
                var container = $('#defaultButtons');
                var data = response.Content;
                var group = [];
                $(data).each(function (i, n) {
                    if ($.inArray(n.showarea, group) < 0) {
                        group.push(n.showarea);
                    }
                });
                var groupNames = ['表单', '列表头部', '列表行内', '单据体'];
                $(group).each(function (j, g) {
                    var groupLine = $('<div class="col-sm-12"></div>');
                    groupLine.append($('<h5>' + groupNames[g - 1] + '</h5>'));
                    $(data).each(function (i, n) {
                        if (g == n.showarea) {
                            groupLine.append($('<label class="col-sm-3"><input type="checkbox" name="defaultbuttons" value="' + n.ribbonbuttonid + '" checked="checked" /> ' + n.label + '</label>'));
                        }
                    });
                    container.append(groupLine);
                });
                container.find('button:eq(0)').click(function () {
                    container.find('input[type=checkbox]').prop('checked', true);
                });
                container.find('button:eq(1)').click(function () {
                    container.find('input[type=checkbox]').removeProp('checked');
                });
            } else {
                Xms.Web.Alert(response.Content);
            }
        })
    }

    //加载引用实体
    function loadEntities() {
        var entitySelect = $('#ParentEntityId');
        entitySelect.entitySelector({});
    }
</script>
</body>
</html>